package demo.msa.retrofit.hello.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.Retrofit;

import java.io.IOException;
import java.lang.reflect.Method;

@Component
public class HelloClient {

    private HelloService helloService;

    @Autowired
    public HelloClient(Retrofit retrofit) {
        helloService = retrofit.create(HelloService.class);
    }

    public String say1(String name) throws IOException {
        return helloService.say1(name).execute().body();
    }

    public String say2(String name) throws IOException {
        return helloService.say2(name).execute().body();
    }

    public String say3(String name) throws IOException {
        return helloService.say3(name).execute().body();
    }

    public String say4() throws IOException {
        return helloService.say4().execute().body();
    }

    public String say5(String... names) throws IOException {
        return helloService.say5(names).execute().body();
    }

    public String say6(String name, int age) throws IOException {
        return helloService.say6(name, age).execute().body();
    }

    public String say7(String name, int age) throws IOException {
        return helloService.say7(name, age).execute().body();
    }

    public String invoke(String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Exception {
        Class clazz = HelloService.class;
        Method method = clazz.getMethod(methodName, parameterTypes);
        method.setAccessible(true);
        Call<String> okHttpCall = (Call<String>)method.invoke(helloService, parameters);
        return okHttpCall.execute().body();
    }
}
